// pages/authorization/authorization.js
var app = require('../../app/app.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    globalData: {
      userInfo: {},
      appid: 'wxf866177a4a1629dd', // appid
      secret: 'cc707be5e544cf9599a90442a276f7bc', // secret
    },
    storageData: {}
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '授权登录'
    })
  },
  loginBack:function() {
   wx.switchTab({
     url: '/pages/home/index/index',
   })
  },
  /**
   * 获取用户信息
   */

  getUserInfo(res) { 
    var that = this
    // console.log(res.detail.userInfo)
    this.data.globalData.userInfo.avatarUrl = res.detail.userInfo.avatarUrl;
    this.data.globalData.userInfo.nickName = res.detail.userInfo.nickName;
    // 把获取的用户信息放在本地缓存中
    wx.setStorageSync('userReInfo', this.data.globalData.userInfo)
    if (this.data.globalData.userInfo) {
      var that = this
      wx.login({
        success(res) {
          if (res.code) {
            // console.log(res.code)
            wx.request({
              url: app.Authorization,
              data: {
                code: res.code,
                avatarUrl: that.data.globalData.userInfo.avatarUrl,
                nickName: that.data.globalData.userInfo.nickName
              },
              method: 'POST',
              header: {
                'content-Type': 'application/x-www-form-urlencoded' // post请求头
              },
              success: function (res) {
                // console.log(res.data)
                that.data.globalData.userInfo.userId = res.data.userdata.user_id
                that.data.globalData.userInfo.avatarUrl = res.data.userdata.user_headimg
                that.data.globalData.userInfo.nickName = res.data.userdata.user_name
                that.data.globalData.userInfo.openId = res.data.userdata.user_openid
                that.data.globalData.userInfo.phone = res.data.userdata.user_iphone
                // 把获取的用户信息放在本地缓存中
                wx.setStorageSync('userReInfo', that.data.globalData.userInfo)
                if (!(res.data == '{}')) {
                  wx.redirectTo({
                    url: '/pages/load/login/login'
                  })
                }
              }
            })
          } else {
            console.log('登录失败！' + res.errMsg)
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.hideHomeButton({
      success: function() {}
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})
